package com.itwlj.controller.user;

import com.itwlj.constant.MessageConstant;
import com.itwlj.constant.TypeConstant;
import com.itwlj.dto.PasswordUpdateDTO;
import com.itwlj.dto.UserDTO;
import com.itwlj.dto.UserLoginDTO;
import com.itwlj.exception.CaptchaErrorException;
import com.itwlj.exception.CaptchaNotFoundException;
import com.itwlj.result.Result;
import com.itwlj.service.UserService;
import com.itwlj.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理
 */
@RestController
@Api(tags = "C端用户相关接口")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("员工登录：{}", userLoginDTO);
        userLoginDTO.setType(TypeConstant.USER);
        UserLoginVO userLoginVO = userService.login(userLoginDTO);
        return Result.success(userLoginVO);
    }

    /**
     * 创建账号
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/account/create")
    @ApiOperation("创建账号")
    public Result<UserLoginVO> createAccount(@RequestBody UserDTO userDTO) {
        log.info("创建账号：{}", userDTO);
        checkCaptcha(userDTO.getPhone(), userDTO.getCaptcha());
        userService.createAccount(userDTO);
        return Result.success();
    }

    /**
     * 校验验证码
     *
     * @param phone
     * @param captcha
     * @return
     */
    @PostMapping("/account/captcha/check")
    @ApiOperation("校验验证码")
    public Result<String> checkCaptcha(String phone, String captcha) {
        log.info("手机号：{}，验证码：{},", phone, captcha);
        // 从 Redis 中获取存储的验证码
        String captchaValue = (String) redisTemplate.opsForValue().get(phone);
        if (captchaValue == null) {
            throw new CaptchaNotFoundException(MessageConstant.CAPTCHA_NOT_FOUND);
        }
        if (!captchaValue.equals(captcha)) {
            throw new CaptchaErrorException(MessageConstant.CAPTCHA_ERROR);
        }
        return Result.success();
    }

    /**
     * 根据用户名和手机号查询用户
     *
     * @param account
     * @param phone
     * @return
     */
    @GetMapping("/account/validate")
    @ApiOperation("根据用户名和手机号查询用户")
    public Result<String> getByAccountAndPhone(String account, String phone) {
        log.info("账号：{}，手机号：{},", account, phone);
        userService.getByAccountAndPhone(account, phone);
        return Result.success();
    }

    /**
     * 重置密码
     *
     * @param passwordUpdateDTO
     * @return
     */
    @PutMapping("/account/password/reset")
    @ApiOperation("重置密码")
    public Result<String> resetPassword(@RequestBody PasswordUpdateDTO passwordUpdateDTO) {
        log.info("账号：{}，重置密码：{}", passwordUpdateDTO.getAccount(),passwordUpdateDTO.getNewPassword());
        userService.resetPassword(passwordUpdateDTO);
        return Result.success();
    }
}
